package com.klaus.ohMySpace.sys.common;

import java.util.ArrayList;
import java.util.List;

/**
 * 返回错误码枚举
 *
 * Created by Mklaus on 15/8/12.
 */
public enum ErrorCode {
    needLogin(-1,"Need Login"),
    missParameter(-2,"Miss Parameter"),
    userNotExist(-3,"user not exist"),
    emailBeTake(-4,"email has been used");

    private static List<ErrorCode> enumList = new ArrayList<ErrorCode>();

    static {
        enumList.add(needLogin);
        enumList.add(missParameter);
        enumList.add(userNotExist);
        enumList.add(emailBeTake);
    }

    private int code;
    private String desc;

    ErrorCode(int code,String desc) {
        this.code = code;
        this.desc = desc;
    }

    public int getCode(){
        return code;
    }

    public String getDesc(){
        return desc;
    }

    @Override
    public String toString() {
        return "errcode : "+ code + ", errMsg : " + desc;
    }

    public ErrorCode getEnum(int code){
        for(ErrorCode ec : enumList){
            if(ec.getCode() == code){
                return ec;
            }
        }
        return null;
    }

    public String getMsg(int code){
        for (ErrorCode ec : enumList){
            if (ec.getCode() == code){
                return ec.getDesc();
            }
        }

        return null;
    }

    public List<ErrorCode> getEnumList(){
        return this.enumList;
    }
}
